// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.web.browser.dom;

import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.ole.win32.GUID;
import org.eclipse.swt.internal.ole.win32.IDispatch;

/**
 * 
 * @author jliu
 */
public class ElementFactory {

    public static final GUID IIDHTMLElement = IIDFromString("{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}");

    /**
     * Create a new NodeList element.
     * 
     * @param dispatch
     * @param context
     * @return
     */
    public static NodeList createNodeList(IDispatch dispatch, DocumentContext context) {
        return new NodeList(dispatch, context);
    }

    /**
     * Create a new Node.
     * 
     * @param dispatch
     * @param context
     * @return
     */
    public static Node createNode(IDispatch dispatch, DocumentContext context) {
        Node node = null;
        if (supportHTML(dispatch)) {
            node = new HTMLElement(dispatch, context);
        } else {
            node = new Node(dispatch, context);
        }
        return node;
    }

    /**
     * Create HTML Document element.
     * 
     * @param dispatch
     * @param context
     * @return
     */
    public static HTMLDocument createDocument(IDispatch dispatch, DocumentContext context) {
        return new HTMLDocument(dispatch, context);
    }

    /**
     * return true if this guid is supported by the unknown element.
     */
    private static boolean supportHTML(IDispatch unknown) {
        int ppvObject[] = new int[1];
        int result = unknown.QueryInterface(IIDHTMLElement, ppvObject);
        if (result == 0) {
            (new IDispatch(ppvObject[0])).Release();
            return true;
        } else {
            return false;
        }
    }

    /**
     * Copied from COM.
     * 
     * @param lpsz
     * @return
     */
    private static GUID IIDFromString(String lpsz) {
        int length = lpsz.length();
        char buffer[] = new char[length + 1];
        lpsz.getChars(0, length, buffer, 0);
        GUID lpiid = new GUID();
        if (COM.IIDFromString(buffer, lpiid) == 0) {
            return lpiid;
        } else {
            return null;
        }
    }
}
